//
//  WeatherView.m
//  quickDoctor
//
//  Created by zhaowei on 17/3/8.
//  Copyright © 2017年 SWS. All rights reserved.
//

#import "WeatherView.h"
#import "LocationTableController.h"
#import "CityTableViewController.h"

@interface WeatherView()


@end
@implementation WeatherView {
    NSDictionary *_weatherDict;
    
    
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if(self =[super initWithFrame:frame]) {
        
        [self loadData];
        
        

    }
    return self;
    
}

- (void)loadData {
    if(_locaName == nil) {
        [self GETWithLocation:@"上海"];
    }else {
        [self GETWithLocation:_locaName];
    }
}
/// 请求数据
- (void)GETWithLocation:(NSString *)str {
    
    NSString *urlStr = @"http://api.map.baidu.com/telematics/v3/weather";
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"location"] = str;
    parameters[@"output"] = @"json";
    parameters[@"ak"] = @"17IvzuqmKrK1cGwNL6VQebF9";
    
    [[NETWorkTool sharedTool] GETWithUrlStr:urlStr parameters:parameters success:^(NSDictionary *responceObject) {
        // 获取字典数组
        NSArray *resultArr = responceObject[@"results"];
        //取出里面对应的天气数组
        NSDictionary *dict = resultArr.lastObject;
        //        NSDictionary *dict = dictArr[0];
        NSArray *weatherArr = dict[@"weather_data"];
        NSDictionary *weatherDict = weatherArr.firstObject;
        _weatherDict = weatherDict;
        
       [self setupUI];
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        [self setupUI];
    }];
    
    
    
}

/*
 请求地址：http://api.map.baidu.com/telematics/v3/weather
 请求参数：（city填什么，大家都懂的！）{@"location":city,@"output":@"json",@"ak":@"17IvzuqmKrK1cGwNL6VQebF9"};
 
 */

/*
 {
 date = "\U5468\U56db 03\U670809\U65e5 (\U5b9e\U65f6\Uff1a16\U2103)";
 dayPictureUrl = "http://api.map.baidu.com/images/weather/day/qing.png";
 nightPictureUrl = "http://api.map.baidu.com/images/weather/night/duoyun.png";
 temperature = "17 ~ 8\U2103";
 weather = "\U6674\U8f6c\U591a\U4e91";
 wind = "\U897f\U5357\U98ce\U5fae\U98ce";
 }
 
 */
//53 193 189

- (void)setupUI{
    //添加imageView
    UIImageView *imageView = [[UIImageView alloc] init];
    NSString *urlStr = _weatherDict[@"dayPictureUrl"];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSData *data = [NSData dataWithContentsOfURL:url];
    imageView.image = [UIImage imageWithData:data];
    [self addSubview:imageView];
    
    //添加天气状况label
    UILabel *weaLabel = [[UILabel alloc] init];
    NSString *urlStr1 = _weatherDict[@"weather"];
    weaLabel.text = urlStr1;
    weaLabel.font = [UIFont systemFontOfSize:13];
    weaLabel.textColor = RGBACOLOR(53, 193, 189, 1);
    [self addSubview:weaLabel];
    
    //添加地点button
    UIButton *locationButton = [[UIButton alloc] init];
    if(_locaName ==nil) {
    [locationButton setTitle:@"上海" forState:UIControlStateNormal];
    }else {
        [locationButton setTitle:_locaName forState:UIControlStateNormal];
    }
    locationButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [locationButton setTitleColor:RGBACOLOR(53, 193, 189, 1) forState:UIControlStateNormal];
    [locationButton addTarget:self  action:@selector(notification) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:locationButton];
    
    //添加细线
    UIImageView *lineImageView = [[UIImageView alloc] init];
    lineImageView.image = [UIImage imageNamed:@"fengexian"];
    [self addSubview:lineImageView];
    
    //添加温度label
    UILabel *temperatureLabel = [[UILabel alloc] init];
    temperatureLabel.text = @"温度:";
    temperatureLabel.textColor = RGBACOLOR(53, 193, 189, 1);
    temperatureLabel.font = [UIFont systemFontOfSize:12];
    [temperatureLabel sizeToFit];
    [self addSubview:temperatureLabel];
    
    //添加度数label
    UILabel *duLabel = [[UILabel alloc] init];
    NSString *duUrlStr = _weatherDict[@"temperature"];
    duLabel.text = duUrlStr;
    [duLabel sizeToFit];
    duLabel.textColor = RGBACOLOR(53, 193, 189, 1);
    [self addSubview:duLabel];
    
    //添加日期label
    UILabel *dateLabel = [[UILabel alloc] init];
    dateLabel.text = @"日期:";
    dateLabel.textColor = RGBACOLOR(53, 193, 189, 1);
    dateLabel.font = [UIFont systemFontOfSize:12];
    [self addSubview:dateLabel];
    
    //添加具体时间label
    UILabel *concreteDateLabel = [[UILabel alloc] init];
    
    [concreteDateLabel sizeToFit];
    
    NSString *dateUrlStr = _weatherDict[@"date"];
    concreteDateLabel.text = dateUrlStr;
    concreteDateLabel.font = [UIFont systemFontOfSize:11];
    concreteDateLabel.textColor = RGBACOLOR(53, 193, 189, 1);
    [self addSubview:concreteDateLabel];
    
    
    //约束
    //imageView
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.width.mas_equalTo(40);
        make.leading.offset(10);
        make.height.mas_equalTo(40);
    }];
    //天气label
    [weaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(imageView.mas_trailing).offset(5);
        make.centerY.equalTo(self);
        make.width.mas_equalTo(60);
    }];
    //细线
    [lineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(60);
        make.centerX.centerY.equalTo(self);
        make.width.mas_equalTo(1);
    }];
    //地点
    [locationButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.trailing.equalTo(lineImageView.mas_leading).offset(-10);
        make.width.mas_equalTo(40);
    }];
    //温度label
    [temperatureLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(lineImageView.mas_trailing).offset(30);
        make.top.offset(8);
        make.width.mas_equalTo(40);
    }];
    //具体温度
    [duLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(temperatureLabel.mas_trailing).offset(3);
        make.centerY.equalTo(temperatureLabel);
        
    }];
    //日期
    [dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(temperatureLabel.mas_bottom).offset(5);
        make.leading.equalTo(lineImageView.mas_trailing).offset(10);
        make.width.mas_equalTo(30);
        
    }];
    //具体时间
    [concreteDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(duLabel.mas_bottom).offset(10);
        make.centerY.equalTo(dateLabel);
        make.leading.equalTo(dateLabel.mas_trailing).offset(5);
        
    }];
    
    
}
/// 发送通知
- (void)notification {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"push" object:nil];

    
}
@end
